package com.t2t.top.zkClient;

import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

import java.util.UUID;


public class App {

    private ZkClient zkClient;

    public ZkClient getZkClient() {
        return zkClient;
    }

    public void setZkClient(ZkClient zkClient) {
        this.zkClient = zkClient;
    }


    /**
     * 函数入口
     *
     * @param args
     */
    public static void main(String[] args) {
        App bootStrap = new App();
        bootStrap.initialize();
        System.out.println("ok");
    }

    /**
     * 初始化zookeeper
     */
    public void initialize() {
        String connectionString = Constant.ZK_ADDRESS;
        int connectionTimeout = 50000;
        zkClient = new ZkClient(connectionString, connectionTimeout);
        if (!zkClient.exists(Constant.ZK_PATH)) {
            zkClient.create(Constant.ZK_PATH, new Long(System.currentTimeMillis()), CreateMode.EPHEMERAL);
        }
        rootNodeChangeThread();
    }

    /**
     * 每20s改变一次 'root1'节点的数据
     */
    public void rootNodeChangeThread() {
        while (true) {
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                //ignore
            }
            String uuidStr = UUID.randomUUID().toString();
            System.out.println(">>>>>>>>>> 产生随机的 uuid string,'uuidStr'===>" + uuidStr);
            zkClient.writeData(Constant.ZK_PATH, uuidStr);
        }

    }
}